package com.example.a001.part09;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.a001.R;

import java.util.Locale;

public class yybb extends AppCompatActivity {

    private TextToSpeech textToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_yybb);

        // 初始化 TextToSpeech
        textToSpeech = new TextToSpeech(this, status -> {
            if (status == TextToSpeech.SUCCESS) {
                Log.d("TextToSpeech", "Initialization successful");
                int result = textToSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TextToSpeech", "Language not supported or missing data");
                    Toast.makeText(yybb.this, "语音播报不支持当前语言", Toast.LENGTH_SHORT).show();
                } else {
                    Log.d("TextToSpeech", "Language set successfully");
                }
            } else {
                Log.e("TextToSpeech", "Initialization failed");
                Toast.makeText(yybb.this, "初始化语音播报失败", Toast.LENGTH_SHORT).show();
            }
        });

        // 找到布局中的视图
        EditText etUsername = findViewById(R.id.et_username);
        EditText etPassword = findViewById(R.id.et_password);
        Button btnLogin = findViewById(R.id.btn_login);
        Button btnTestSpeech = findViewById(R.id.btn_test_speech);

        // 设置登录按钮的点击事件监听器
        btnLogin.setOnClickListener(v -> {
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (username.isEmpty() || password.isEmpty()) {
                speak("请输入账号和密码");
            } else {
                if (username.equals("123456") && password.equals("123456")) {
                    Toast.makeText(yybb.this, "登录成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(yybb.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 设置测试语音按钮的点击事件监听器
        btnTestSpeech.setOnClickListener(v -> speak("测试语音功能"));

        // 处理窗口边距
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    @Override
    protected void onDestroy() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onDestroy();
    }

    /**
     * 封装语音播报方法
     *
     * @param text 要播报的文本
     */
    private void speak(String text) {
        if (textToSpeech != null) {
            textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
        }
    }
}
